package com.yanqu.road.server.logic.activity.condition.normal;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/10/8 15:18
 */
public class ServerOpenGiftsCondition extends BaseActivityCondition {

    public ServerOpenGiftsCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.ServerOpenGifts.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.ServerOpenGifts.getValue(), handle);
    }

    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        int mallId = (int)object;
        for (String paramStr : activityConditionInfo.getExtendParam().split("\\|")) {
            String[] param = paramStr.split(";");
            if (Integer.parseInt(param[0]) == mallId) {
                MallInfo mallInfo = MallMgr.getMallInfoByMallId(mallId);
                if (mallInfo != null) {
                    int mul = Integer.parseInt(param[1]);
                    Property rewardProperty = new Property();
                    for (Map.Entry<Integer, BigInteger> entry : mallInfo.getRewardProp().getGoods().entrySet()) {
                        if (entry.getKey() == GameConfig.GAME_MONEY_INGOTS) {
                            rewardProperty.addProperty(entry.getKey(), entry.getValue().multiply(BigInteger.valueOf(mul)));
                        } else {
                            rewardProperty.addProperty(entry.getKey(), entry.getValue());
                        }
                    }
                    setValue(BigInteger.ONE);
                    player.getModule(CurrencyModule.class).addCurrency(rewardProperty, eLogMoneyType.Recharge, eLogMoneyType.ReChargeGet);
                    getReward();
                }
            }
        }
    }
}
